<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <style type="text/css">

    </style>

</head>
<body>
<div class="page-return">
    <div class="ops-form">
        <div class="page-head">
            <form class="form-horizontal tasi-form" method="post" id="params" action="">
                <div class="form-group">
                    <label class="col-sm-1 control-label">业务字段</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" name="businessKey" id="businessKey" value="">
                    </div>
                    <label class="col-sm-1 control-label">流程名称</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" name="processDefinitionName" id="processDefinitionName" value="">
                    </div>
                    <div class="col-sm-3">
                        <button type="button" class="btn btn-info" onclick="" id="historyProcessSearch">搜索</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="ops-table" >
        <caption>
            <div class="ops-edit">
            </div>
        </caption>
        <div class="ops-table-content">
            <table id="historyProcessTable"></table>
        </div>
    </div>
</div>
</body>
<script>
    $(function() {
        var historyProcessTable = $("#historyProcessTable").bootstrapTable({ // 对应table标签的id
            url: "/workflow/historyprocess/list", // 获取表格数据的url
            queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                var paramOption = {
                    pageSize: params.limit, // 每页要显示的数据条数
                    pageNumber: (params.offset / params.limit) + 1, // 每页显示数据的开始行号
                    sortName: params.sort, // 要排序的字段
                    searchText: '',//搜索内容
                    sortOrder: params.order, // 排序规则
                    dataId: $("#dataId").val() // 额外添加的参数
                }
                return paramOption;
            },
            columns: [
                {
                    field: 'processDefinitionName',
                    title: '流程名称',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center'
                },
                {
                    field: 'businessKey',
                    title: '业务字段',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center'
                },
                {
                    field: 'startUserId',
                    title: '发起人',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center'
                },
                {
                    field: 'processDefinitionVersion',
                    title: '流程版本',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center',
                    formatter: function (value, row, index) {
                        return 'v'+value;
                    }
                },
                {
                    field: 'startTime',
                    title: '开始时间',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center',
                    formatter: function (value, row, index) {
                        return new Date(value).format("yyyy-MM-dd hh:mm:ss");
                    }
                },
                {
                    field: 'endTime',
                    title: '结束时间',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center',
                    formatter: function (value, row, index) {
                        return new Date(value).format("yyyy-MM-dd hh:mm:ss");
                    }
                },
                {
                    field: 'durationInMillis',
                    title: '用时',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center',
                    formatter: function (value, row, index) {
                        return (value/(1000*60*60)).toFixed(2)+'时'
                    }
                },
                {
                    field: 'deleteReason',
                    title: '状态',
                    align: 'center',
                    valign: 'middle',
                    halign: 'center',
                    formatter: function (value, row, index) { // 单元格格式化函数
                        var text = '';
                        if (row.deleteReason) {
                            text = '<h7 style="color:red">[作废]'+row.deleteReason+'</h7>';
                        } else {
                            text = '<h7 >正常结束</h7>';
                        }
                        return text;
                    }
                },
                {
                    title: "操作",
                    width: 120, // 定义列的宽度，单位为像素px
                    formatter: function (value, row, index) {
                        return '<button class="btn btn-xs btn-info" onclick="viewhistoryProcess(\'' + row.id + '\')">流程信息</button> &nbsp;'
                            + '<button class="btn btn-xs btn-danger" onclick="delhistoryprocess(\'' + row.id + '\')"><i class="ace-icon fa fa-trash-o bigger-110" title="删除"></i></button>   ';
                    }
                }
            ]
        });
    });

    /**
     * 删除历史流程
     * @param processInstanceId 流程实例id
     */
    function delhistoryprocess(processInstanceId) {
        layer.confirm('历史流程删除后，用户将无法看到与该流程相关联的历史任务！谨慎操作！</br>确认删除？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type:"POST",
                url:'/workflow/historyprocess/delete',
                data: {processInstanceId:processInstanceId},
                dataType:"json",
                success:function(data){
                    if(data.resCode == '200'){
                        parent.$("#historyProcessTable").bootstrapTable('refresh',{silent: true});
                        parent.layer.msg(data.resMessage,{time:1200});
                        layer.close(index);
                        return true;
                    }else {
                        parent.layer.msg(data.resMessage);
                        return false;
                    }
                },
                error:function(data){
                    parent.layer.msg("错误："+data.resMessage);
                }
            })
        }, function(){
            layer.msg('取消操作', {
                time: 1500 //1.5s后自动关闭
            });
        });
    }

    /**
     * 预览流程
     * @param processInstanceId 流程实例Id
     */
    function viewhistoryProcess(processInstanceId){
        layer.open({
            type: 2,
            title: '流程信息',
            shadeClose: true,
            shade: false,
            maxmin: true,
            area: ['1100px', '599px'],
            content: '/workflow/historyprocess/processinfo/?processInstanceId='+processInstanceId
        });
    }
    /**
     *  搜索按钮
     */
    $("#historyProcessSearch").click(function () {
        $("#historyProcessTable").bootstrapTable('refresh',{
            silent: true,
            query: {
                businessKey:$("#businessKey").val(),
                processDefinitionName:$("#processDefinitionName").val()
            }
        });
    });
</script>
</html>